﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Fan.Dongping.BusinessRules;
using Fan.Dongping.DataAccess;
using Fan.Dongping.Common.DBMeta;
using Fan.Dongping.BusinessRules.Resources;


public partial class AccountLogin : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            // lnkDefaultPage.NavigateUrl = "Comm";
        }
    }

    protected void btLogin_Click(object sender, EventArgs e)
    {
        if (txtUserName.Text.Length == 0)
        {
            this.labMessage.Text = Fan.Dongping.BusinessRules.Resources.ResString.Error_NameEmpty; // ;
            txtUserName.Focus();
            return;

        }

        //this.Trace.Write(this.User.Identity.Name);
        //如果用户验证正确者转到index.aspx
        //否则显示错误信息
        if (UserBiz.VerifyUserPassword(txtUserName.Text, txtUserPass.Text))
        {
            //UserDA.UpdateLastLogin(this.txtUserName.Text);

            //LogDA.AddLog(LogMeta.InformationType, string.Format("Logined: {0} ,IP: {1}", txtUserName.Text, Request.UserHostAddress));


            string groups = string.Empty;
            //string I = string.Empty;
            //foreach (string s in groupList)
            //{
            //    groups += I;
            //    groups += s;
            //    I = "|";
            //}

            //Create the authentication ticket
            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, txtUserName.Text, DateTime.Now, DateTime.Now.AddMinutes(120), false, groups);

            //switch (lstSaveLogin.SelectedIndex)
            //{
            //    case 0:
            //        authTicket = new FormsAuthenticationTicket(1, txtUserName.Text, DateTime.Now, DateTime.Now.AddMinutes(120), false, groups);
            //        break;
            //    case 1:
            //        authTicket = new FormsAuthenticationTicket(1, txtUserName.Text, DateTime.Now, DateTime.Now.AddDays(1), true, groups);
            //        break;
            //    case 2:
            //        authTicket = new FormsAuthenticationTicket(1, txtUserName.Text, DateTime.Now, DateTime.Now.AddDays(7), true, groups);
            //        break;
            //    case 3:
            //        authTicket = new FormsAuthenticationTicket(1, txtUserName.Text, DateTime.Now, DateTime.Now.AddMonths(1), true, groups);
            //        break;

            //    case 4:
            //        authTicket = new FormsAuthenticationTicket(1, txtUserName.Text, DateTime.Now, DateTime.Now.AddYears(1), true, groups);
            //        break;
            //    default:
            //        authTicket = new FormsAuthenticationTicket(1, txtUserName.Text, DateTime.Now, DateTime.Now.AddMinutes(120), false, groups);
            //        break;

            //}

            // Now encrypt the ticket.
            string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
            // Create a cookie and add the encrypted ticket to the 
            // cookie as data.
            HttpCookie authCookie = new HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName, encryptedTicket);

            //if (lstSaveLogin.SelectedIndex > 0)
            //{
            //    authCookie.Expires = authTicket.Expiration;
            //    authCookie.Path = System.Web.Security.FormsAuthentication.FormsCookiePath;
            //}

            // Add the cookie to the outgoing cookies collection. 
            Response.Cookies.Add(authCookie);

            // Redirect the user to the originally requested page
            string url = FormsAuthentication.GetRedirectUrl(txtUserName.Text, false);
            if (
                url.Contains(@"/default.aspx")
                || url.Contains(@"/Default.aspx")
                || url.Equals(@"/Default.htm", StringComparison.InvariantCultureIgnoreCase)
                || url.Equals(@"/Default.aspx", StringComparison.InvariantCultureIgnoreCase)
                || url.Equals(@"/Title.aspx", StringComparison.InvariantCultureIgnoreCase)

                )
            {
                //如果从首页页面，则转到loginPass页
                url = @"comm";
            }
            Response.Redirect(url);
        }
        else
        {
            txtUserPass.Focus();
            labMessage.Text =ResString. Error_Password;
        }
    }
    protected void LinkButtonLanguage_Click(object sender, EventArgs e)
    {
        WebUtilities.ChangeLanguage(this);
    }
}
